<?php

namespace Biz\service\wikiRanking;

use Biz\model\wiki\WikiRankingCategory;
use Biz\model\wiki\WikiRankingConfig;
use Doraemon\model\Category;
use Doraemon\service\statSign\Basic as statSign;
use Doraemon\tools\BlockType;
use Doraemon\tools\HhzClient;
use Doraemon\tools\ParticipleKeyword;

class Schedule
{
    // 插入品牌榜单
    public static function insertBrandRanking(&$list, $keyword, $requestId, $displayGoodsNum = 3)
    {
        $hasInsert = false;

        $parKeywords = ParticipleKeyword::getCommonParticipleKeyword($keyword);
        $parKeywordArr = $parKeywords ? explode(",", $parKeywords) : array();

        // 取分词后 后者的榜单id
        // 优先获取榜单名字
        $wikiRankId = self::getDataByRankingName($parKeywordArr);

        if (!$wikiRankId) {
            $wikiRankId = self::getDataByRankingCateroty($parKeywordArr);
        }

        if ($wikiRankId) {
            $RankingCards = \Biz\service\wikiRanking\Ranking::multiFormatRankingCard(array($wikiRankId), $displayGoodsNum);

            if (is_array($RankingCards) && $RankingCards) {
                $RankingCard = array_shift($RankingCards);
                $formatRankingCard = array(
                    "type" => BlockType::TYPE_RANKING,
                    "ranking" => array(
                        "ranking_info" => $RankingCard
                    ),
                    "statSign" => statSign::genStatSign(statSign::MODULE_SEARCH_RESULT_RANKING, [
                        statSign::EXTEND_KEY_OBJ_ID => $wikiRankId,
                        statSign::EXTEND_KEY_OBJ_TYPE => BlockType::TYPE_RANKING,
                        statSign::EXTEND_KEY_FROM => "search",
                    ], [
                        "ranking_id" => $wikiRankId,
                        "uid" => HhzClient::getUid(),
                        "search_request_id" => $requestId
                    ])
                );

                $index = rand(5, 9);
                $list = \Doraemon\service\search\Schedule::insertContent($formatRankingCard, $list, $index);
                $hasInsert = true;
            }
        }

        return $hasInsert;
    }

    // 插入 品牌榜单 到 内容列表
    public static function insertBrandRanking2ContentList($formatRankingCard, &$list)
    {
        $index = rand(5, 9);
        $list = \Doraemon\service\search\Schedule::insertContent($formatRankingCard, $list, $index);
    }

    // 获得品牌榜单
    public static function getBrandRanking($keyword, $requestId, $displayGoodsNum = 3)
    {
        $hasInsert = false;

        $parKeywords = ParticipleKeyword::getCommonParticipleKeyword($keyword);
        $parKeywordArr = $parKeywords ? explode(",", $parKeywords) : array();

        // 取分词后 后者的榜单id
        // 优先获取榜单名字
        $wikiRankId = self::getDataByRankingName($parKeywordArr);

        if (!$wikiRankId) {
            $wikiRankId = self::getDataByRankingCateroty($parKeywordArr);
        }

        $formatRankingCard = array();

        if ($wikiRankId) {
            $RankingCards = \Biz\service\wikiRanking\Ranking::multiFormatRankingCard(array($wikiRankId), $displayGoodsNum);

            if (is_array($RankingCards) && $RankingCards) {
                $RankingCard = array_shift($RankingCards);
                $formatRankingCard = array(
                    "type" => BlockType::TYPE_RANKING,
                    "ranking" => array(
                        "ranking_info" => $RankingCard
                    ),
                    "statSign" => statSign::genStatSign(statSign::MODULE_SEARCH_RESULT_RANKING, [
                        statSign::EXTEND_KEY_OBJ_ID => $wikiRankId,
                        statSign::EXTEND_KEY_OBJ_TYPE => BlockType::TYPE_RANKING,
                        statSign::EXTEND_KEY_FROM => "search",
                    ], [
                        "ranking_id" => $wikiRankId,
                        "uid" => HhzClient::getUid(),
                        "search_request_id" => $requestId
                    ])
                );

                $hasInsert = true;
            }
        }

        return array($hasInsert, $formatRankingCard);
    }

    // 通过榜单名字获取榜单信息
    public static function getDataByRankingName($parKeywordArr)
    {
        $wikiRankId = 0;

        // 优先和榜单表的榜单名进行匹配
        $wikiRankNameCon = array(
            "config_state" => WikiRankingConfig::CONFIG_STATE_SHOW,
            "deep" => WikiRankingConfig::DEEP_TWO,
            "ranking_name" => $parKeywordArr
        );

        $wikiRankDatasByName = WikiRankingConfig::selectByWhere($wikiRankNameCon, ["id", "ranking_name"]);
        if (is_array($wikiRankDatasByName) && $wikiRankDatasByName) {
            foreach ($parKeywordArr as $participleName) {
                foreach ($wikiRankDatasByName as $wikiRankData) {
                    if ($wikiRankData["ranking_name"] == $participleName) {
                        $wikiRankId = $wikiRankData["id"];
                    }
                }
            }
        }

        return $wikiRankId;
    }

    // 获取榜单信息通过榜单关联的分类
    public static function getDataByRankingCateroty($parKeywordArr)
    {
        $wikiRankId = 0;

        $categoryInfos = array();
        if (is_array($parKeywordArr) && $parKeywordArr) {
            // 获取二级分类信息
            $CategoryCon = array(
                "cate_name" => $parKeywordArr,
                "status" => Category::STATUS_NORMAL
            );

            $categoryInfos = Category::selectByWhere($CategoryCon, ["id", "cate_name"]);
        }

        // 获取后台分类ids
        $cateIds = array();
        foreach ($parKeywordArr as $parKeyword) {
            foreach ($categoryInfos as $categoryInfo) {
                if ($parKeyword == $categoryInfo["cate_name"]) {
                    if ($categoryInfo["id"]) {
                        array_push($cateIds, $categoryInfo["id"]);
                    }
                }
            }
        }

        // 获取榜单ids
        $WikiRankingCategorys = array();
        if (is_array($cateIds) && $cateIds) {
            $wikiRankCateCon = array(
                "cate_state" => WikiRankingCategory::CAT_STATE_NORMAL,
                "cate_id" => $cateIds
            );

            $WikiRankingCategorys = WikiRankingCategory::selectByWhere($wikiRankCateCon, ["ranking_id", "cate_id"]);
        }

        // 将榜单id 以 分词后的顺序存储
        $rankingIds = array();
        foreach ($cateIds as $cateId) {
            foreach ($WikiRankingCategorys as $WikiRankingCategory) {
                if ($cateId == $WikiRankingCategory["cate_id"]) {
                    array_push($rankingIds, $WikiRankingCategory["ranking_id"]);
                }
            }
        }

        // 获取有用的榜单ids
        $wikiRankDatas = array();
        if (is_array($rankingIds) && $rankingIds) {
            $wikiRankCon = array(
                "config_state" => WikiRankingConfig::CONFIG_STATE_SHOW,
                "deep" => WikiRankingConfig::DEEP_TWO,
                "id" => $rankingIds
            );

            $wikiRankDatas = WikiRankingConfig::selectByWhere($wikiRankCon, ["id", "ranking_name"]);
        }

        foreach ($rankingIds as $rankingId) {
            foreach ($wikiRankDatas as $wikiRankData) {
                if ($rankingId == $wikiRankData["id"]) {
                    $wikiRankId = $rankingId;
                }
            }
        }

        return $wikiRankId;
    }
}